package cjlu.ea.h.controller;


import cjlu.ea.h.common.R;
import cjlu.ea.h.entity.SUser;
import cjlu.ea.h.service.SUserService;
import io.swagger.models.auth.In;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 学生账号信息 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-29
 */
@RestController
@CrossOrigin
@RequestMapping("/s-user")
public class SUserController {

    @Resource
    SUserService service;
    @Resource
    RedisTemplate<String,Object> redisTemplate;


    @PostMapping("/addSUser")
    public R addSUser(@RequestBody SUser sUser){

       boolean result = service.save(sUser);
       if(result){
           return R.ok();
       }else {
           return R.error();
       }


    }
    @GetMapping("/getUserById/{id}")
    public R getUserById(@PathVariable Integer id){
        SUser sUser = null;
        if((sUser=(SUser)redisTemplate.opsForValue().get("user_id"+id))!=null){
            System.out.println("get data from redis");
            return R.ok().data("user_id"+id,sUser);

        }else{
            sUser = service.getById(id);
            if(sUser==null){
                return R.error().message("没有该用户");
            }
            redisTemplate.opsForValue().set("user_id"+id,sUser);
            return R.ok().data("sUser",sUser);
        }

    }


}

